package com.eypg.action;

import java.io.File;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.eypg.pojo.Product;
import com.eypg.pojo.Producttype;
import com.eypg.service.ProductService;
import com.eypg.service.ProductTypeService;
import com.eypg.util.Base64;
import com.eypg.util.StringUtil;
import com.eypg.util.Struts2Utils;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author 作者ryan_song E-mail:song316@gmai.com
 * @version 创建时间：2011-3-3 下午10:12:32
 * 类说明
 */
@Component
public class ProductAction extends ActionSupport{
	private static final long serialVersionUID = -6356307819518359036L;
	
	private List<Producttype> typeList;
	String productName;
	String productPrice;
	String productRealPrice;
	String productTitle;
	String productType;
	File headImage;
	String contentType;
	String fileName;
	String description;
	
	@Autowired
	@Qualifier("productService")
	private ProductService productservice;
	@Autowired
	private ProductTypeService productTypeservice;
	
	/**
	 * 准备新增商品
	 * @return
	 */
	public String pAddProduct(){
		typeList = productTypeservice.queryAllProductType();
		return "pAddProduct";
	}
	public String addProduct(){
		if(StringUtil.isBlank(fileName)){
			Struts2Utils.renderJson("{\"statusCode\":\"300\", \"message\":\"文件上传失败!\"}");
			return null;
		}
		if(contentType.indexOf("jpg") < 0 && contentType.indexOf("jpeg") < 0 
				&&contentType.indexOf("gif") < 0 &&contentType.indexOf("png") < 0){
			Struts2Utils.renderJson("文件类型错误,类型只能是jpg,jpeg,gif,png!");
			return null;
		}
		Product product = new Product();
		if(headImage != null){
			product.setHeadImage("");
		}
		product.setProductDetail(description);
		product.setProductName(productName);
		product.setProductPrice(Integer.parseInt(productPrice));
		product.setProductRealPrice(productRealPrice);
		product.setProductTitle(productTitle);
		product.setProductType(Integer.parseInt(productType));
		productservice.addProduct(product);
		Struts2Utils.renderJson("{\"statusCode\":\"200\", \"message\":\"添加商品成功!\", \"navTabId\":\"\", \"forwardUrl\":\"\", \"callbackType\":\"closeCurrent\"}");
		return null;
	}
	//setter & getter
	public void setHeadImage(File headImage){
		this.headImage = headImage;
	}
	public void setHeadImageFileName(String fileName){
		this.fileName = fileName;
	}
	public void setHeadImageContentType(String contentType){
		this.contentType = contentType;
	}
	public List<Producttype> getTypeList() {
		return typeList;
	}
	public String getProductName() {
		return productName;
	}
	public void setProductName(String productName) {
		this.productName = productName;
	}
	public String getProductPrice() {
		return productPrice;
	}
	public void setProductPrice(String productPrice) {
		this.productPrice = productPrice;
	}
	public String getProductRealPrice() {
		return productRealPrice;
	}
	public void setProductRealPrice(String productRealPrice) {
		this.productRealPrice = productRealPrice;
	}
	public String getProductTitle() {
		return productTitle;
	}
	public void setProductTitle(String productTitle) {
		this.productTitle = productTitle;
	}
	public String getProductType() {
		return productType;
	}
	public void setProductType(String productType) {
		this.productType = productType;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
}
